package com.pangu.web.view.freemarker;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pangu.web.view.AbstractTemplateView;

import freemarker.ext.jsp.TaglibFactory;
import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.ext.servlet.HttpRequestHashModel;
import freemarker.ext.servlet.HttpRequestParametersHashModel;
import freemarker.ext.servlet.HttpSessionHashModel;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;

public class FreemarkerView extends AbstractTemplateView {
	
	private String encoding;
	
	private String path;
	
	private Configuration configuration;
	
	private TaglibFactory taglibFactory;
	
	public FreemarkerView() {}
	
	public FreemarkerView(String path) {
		this.path = path;
	}
	
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	public String getEncoding() {
		return this.encoding;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	public Configuration getConfiguration() {
		return this.configuration;
	}
	
	public void setTaglibFactory(TaglibFactory taglibFactory) {
		this.taglibFactory = taglibFactory;
	}
	
	public TaglibFactory getTaglibFactory() {
		return this.taglibFactory;
	}

	protected void renderMergedTemplateModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Throwable {
		// TODO support jsp taglib
//		model.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
//		model.put(FreemarkerServlet.KEY_APPLICATION, value);
		model.put("NOW", new Date());
		model.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
		model.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
		model.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
		
		if (log.isDebugEnabled())
			log.debug("Rendering FreeMarker template [" + path + "]");
		
		Template tmpl = getTemplate(path);
		tmpl.process(model, response.getWriter());
	}
	
	protected Template getTemplate(String name) throws IOException {
		return (getEncoding() != null) ?
				getConfiguration().getTemplate(name, getEncoding()) :
				getConfiguration().getTemplate(name);
	}
	
	private HttpSessionHashModel buildSessionModel(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			return new HttpSessionHashModel(session, getObjectWrapper());
		}
		else {
			return new HttpSessionHashModel(null, request, response, getObjectWrapper());
		}
	}
	
	protected ObjectWrapper getObjectWrapper() {
		ObjectWrapper ow = getConfiguration().getObjectWrapper();
		return (ow != null) ? ow : ObjectWrapper.DEFAULT_WRAPPER;
	}
}
